package Thread;

import java.util.Scanner;
import java.util.Stack;

public class test2 {
    public static boolean backspaceCompare(String s, String t) {
        Stack<Character> stack1 = new Stack<>();
        Stack<Character> stack2 = new Stack<>();
        for(int i = 0;i < s.length();i++) {
            if(stack1.isEmpty() && s.charAt(i) != '#') {
                stack1.push(s.charAt(i));
                continue;
            }
            if(!stack1.isEmpty()) {
                if(s.charAt(i) == '#') stack1.pop();
                else stack1.push(s.charAt(i));
            }
        }
        for(int i = 0;i < t.length();i++) {
            if(stack2.isEmpty() && t.charAt(i) != '#') {
                stack2.push(t.charAt(i));
                continue;
            }
            if(!stack2.isEmpty()) {
                if(t.charAt(i) == '#') stack2.pop();
                else stack2.push(t.charAt(i));
            }
        }
        boolean flag = false;
        if(stack1.isEmpty() && stack2.isEmpty()) flag = true;
        while(!stack1.isEmpty() && !stack2.isEmpty()) {
            if(stack1.pop() == stack2.pop()) flag = true;
            else flag = false;
        }
        if (stack1.isEmpty() && !stack2.isEmpty() || !stack1.isEmpty() && stack2.isEmpty()) flag = false;
        return flag;
    }
    public static void main(String[] args) {
        String s = "bxj##tw";
        String t = "bxj###tw";
        System.out.println(backspaceCompare(s, t));
    }
}
